package novel.spider.impl.novel;

import novel.spider.NovelSiteEnum;
import novel.spider.entitys.Novel;
import novel.spider.impl.AbstractSpider;
import novel.spider.interfaces.INovelSpider;
import novel.spider.util.NovelSpiderUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.util.List;
import java.util.Map;

/**
 * 抽象小说列表抓取，已实现解析tr元素方法
 * Created by Kau on 2016/11/5.
 */
public abstract class AbstractNovelSpider extends AbstractSpider implements INovelSpider {

    /**
     * 默认抓取方法，最多尝试 {@link INovelSpider#MAX_TRY_TIMES} 次下载
     * @param url
     * @return
     * @throws Exception
     */

    protected Elements getsTr(String url) throws Exception{
        return getsTr(url,INovelSpider.MAX_TRY_TIMES);
    }

    /**
     * 以指定次数形式解析对应网页
     * @param url
     * @param maxTryTimes 可以为空，默认值为 {@link INovelSpider#MAX_TRY_TIMES}
     * @return
     * @throws Exception
     */
    protected Elements getsTr(String url, Integer maxTryTimes) throws Exception{
        maxTryTimes = maxTryTimes == null ? INovelSpider.MAX_TRY_TIMES : maxTryTimes;
        Elements trs = null;
        for (int i = 0; i < maxTryTimes; i++){
            try {
                String result = super.crawl(url);
                Map<String, String> context = NovelSpiderUtil.getContext(NovelSiteEnum.GetEnumByUrl(url));
                String novelSelector = context.get("novel-selector");
                if (novelSelector == null) throw new RuntimeException(NovelSiteEnum.GetEnumByUrl(url) + "URL=" + url + "目前不支持抓取");
                Document doc = Jsoup.parse(result);
                doc.setBaseUri(url);
                trs = doc.select(novelSelector);
                return trs;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        throw new RuntimeException(url + ",尝试了" + maxTryTimes + "次下载仍旧失败！");
    }


    @Override
    public List<Novel> GetsNovel(String url) {
        return null;
    }
}
